package com.yuanlp.studentsystem;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.yuanlp.studentsystem.bean.StudentInfo;
import com.yuanlp.studentsystem.dao.StudentInfoDao;

import java.util.ArrayList;
import java.util.List;

import static com.yuanlp.studentsystem.R.id.tv_item_id;
import static com.yuanlp.studentsystem.R.id.tv_item_name;
import static com.yuanlp.studentsystem.R.id.tv_item_phone;

public class MainActivity extends AppCompatActivity {

    private EditText et_id;
    private EditText et_name;
    private EditText et_phone;
    private ListView lv;
    private List<StudentInfo> mList;
    private StudentInfoDao mStudentInfoDao=new StudentInfoDao(this);

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        //1、找到控件
        //2、模拟操作，添加假数据进行显示
        //3、去除假数据，进行真实显示
        //4、将数据写入到数据库
        //5、将数据库文件上传到服务器

        initView();
    }

    public void initView(){
        et_id = (EditText) findViewById(R.id.et_id);
        et_name = (EditText) findViewById(R.id.et_name);
        et_phone = (EditText) findViewById(R.id.et_phone);
        lv = (ListView) findViewById(R.id.lv);


        mList = new ArrayList<StudentInfo>();
//        for (int i=0;i<100;i++){
//            StudentInfo student=new StudentInfo();
//            student.setId(i);
//            student.setName("student"+i);
//            student.setPhone("phone"+i);
//            mList.add(student);
//        }

        //创建一个adapter对象，集成baseAdapter并实现其方法
        myAdapter myadapter=new myAdapter();
        //展示listview内容
        lv.setAdapter(myadapter);

    }

    /**
     * button的点击方法
     */
    public void addStudent(View view){
        String id=et_id.getText().toString().trim();
        String name=et_name.getText().toString().trim();
        String phone=et_phone.getText().toString().trim();
        StudentInfo student=new StudentInfo();
        student.setId(Integer.valueOf(id));
        student.setName(name);
        student.setPhone(phone);
        mStudentInfoDao.addStudent(student);
        mList.add(student);
        myAdapter myadapter=new myAdapter();
        //展示listview内容
        lv.setAdapter(myadapter);

    }

    private class myAdapter extends BaseAdapter {

        @Override
        public int getCount() {
            //要展示的listview的数量
            return mList.size();
        }

        @Override
        public StudentInfo getItem(int position) {
            return mList.get(position);
        }

        @Override
        public long getItemId(int position) {
            return 0;
        }

        @Override
        public View getView(final int position, View convertView, ViewGroup parent) {
            ViewHolder holder=null;  //新建一个公共的控件类型类，里面包含每个listview要展示的控件类型

            if (convertView==null){  //如果convertview为null，那么就重新生成一个view给他

                holder=new ViewHolder();

                //获取一个子布局的view对象
                View view=LayoutInflater.from(MainActivity.this).inflate(R.layout.item,null);

                convertView= view;

                holder.id= (TextView)convertView. findViewById(tv_item_id);
                holder.name= (TextView) convertView.findViewById(tv_item_name);
                holder.phone= (TextView) convertView.findViewById(tv_item_phone);
                holder.img= (ImageView) convertView.findViewById(R.id.iv_delete);

                convertView.setTag(holder);  //将holder绑定到convertview的tag上，方便后面回收时使用

            }else{
                //如果convertview是回收的view，那么就取出之前放到convertview中的tag对应的holder
                holder= (ViewHolder) convertView.getTag();

            }


            /**
             * listview中图片的点击事件
             */
            holder.img.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {

                    mList.remove(position);
                    lv.setAdapter(new myAdapter());
                }
            });

            //给3个控件赋值
            StudentInfo student=mList.get(position);
             holder.id.setText(student.getId()+"");
            holder.name.setText(student.getName());
            holder.phone.setText(student.getPhone());

            return convertView;
        }
    }

    /**
     * 公共的控件类，里面包含每一行要显示的控件
     */
    private class ViewHolder{
        public TextView id;
        public TextView name;
        public TextView phone;
        public ImageView img;
    }

}
